<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Host extends AdminBase {
	public function __construct()
    {
        parent::__construct();
    }

	public function index()
	{
		$post = $this->input->post();
		if($post){
			if(isset($post['id'])){//name order 
				if(isset($post['name'])){//修改名称
					if(mb_strlen($post['name'],"UTF8")>50 || mb_strlen($post['name'],"UTF8")<8){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'域名为8-50个字符！')));
						return;
					}
					$repeat = $this->db->get_where('host',['name'=>$post['name'],'id !='=>$post['id']])->row_array();
					if($repeat){
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'failed','message'=>'域名已存在！')));
						return;
					}
					$this->db->where('id',$post['id']);
					$this->db->update('host',$post);
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'ok','message'=>'修改成功！')));
					return;
				}else{
					if(isset($post['order'])){//排序
						$this->db->where('id',$post['id']);
						$this->db->update('host',['order'=>$post['order']]);
						$this->output->set_content_type('application/json')
						 ->set_output(json_encode(array('statu' => 'ok','message'=>'排序成功！')));
						return;
					}else{//删除  检查是否有属于该语言的商品
						if($post['id']==1){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'删除失败，请保留主域名！')));
							return;
						}
						$product = $this->db->get_where('product',['host_id'=>$post['id']])->row_array();
						if($product){
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'failed','message'=>'删除失败，请先修改或删除属于该域名的商品！')));
							return;
						}else{
							$this->db->delete('host', array('id' => $post['id']));
							$this->output->set_content_type('application/json')
							 ->set_output(json_encode(array('statu' => 'ok','message'=>'删除成功！')));
							return;
						}
					}
				}
			}else{ //新增
				if(mb_strlen($post['name'],"UTF8")>50 || mb_strlen($post['name'],"UTF8")<8){
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'failed','message'=>'域名为8-50个字符！')));
					return;
				}
				$repeat = $this->db->get_where('host',['name'=>$post['name']])->row_array();
				if($repeat){
					$this->output->set_content_type('application/json')
					 ->set_output(json_encode(array('statu' => 'failed','message'=>'域名已存在！')));
					return;
				}
				$this->db->select('order');
				$this->db->order_by('order', 'DESC');
				$max = $this->db->get('host')->row_array();
				$post['order'] = $max?$max['order']+1:0;
				$this->db->insert('host',$post);
				$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'ok','message'=>'新增成功！')));
				return;
			}
		}else{
			$this->db->order_by('order','ASC');
	        $host = $this->db->get('host')->result_array();
			$this->admin_view('host_list','域名管理',array('list'=>$host));
		}


        
	}
}
